package Top_Interview_Questions.Math;

/**
 * @Author: 吕庆龙
 * @Date: 2020/3/11 11:55
 * <p>
 * 功能描述:
 */
public class _0050 {
    public static void main(String[] args) {
        _0050 test = new _0050();
        double myPow1 = test.myPow1(2, 9);
        System.out.println(myPow1);
    }


    /**
     * 快速幂
     */
    public double myPow1(double x, int n) {
        long N = n;
        if (N < 0) {
            x = 1 / x;
            N = -N;
        }
        double ans = 1;
        double current_product = x;
        for (long i = N; i > 0; i /= 2) {
            if ((i % 2) == 1) {
                ans = ans * current_product;
            }
            current_product = current_product * current_product;
        }
        return ans;
    }




    /**
     * 暴力算法不香嘛
     */
    public double myPow(double x, int n) {
        long N = n;
        if (N < 0) {
            x = 1 / x;
            N = -N;
        }
        double ans = 1;
        for (long i = 0; i < N; i++)
            ans = ans * x;
        return ans;
    }

}
